var express = require('express');
var jwt = require('jwt-simple');
var moment = require('moment');
var app = express();


/*let utils = {

}*/

class utils {

    // 生成token
    generateToken(data) {
        let expires = moment().add(7, 'days').valueOf();
        var token = jwt.encode(
            {
                iss: data[0].username,
                exp: expires
            }, 'sercet');
            return token;
    }

    //
    decodeToken(token) {
        let decoded = jwt.decode(token, 'sercet');
        if (decoded.exp <= Date.now()) {
            console.log('已过期');
            return false
        } else {
            console.log('success')
            return true
        }
    }
}
/*var expires = moment().add(7, 'days').valueOf();
var token = jwt.encode({
  iss: {name: '123', id: '11'},
  exp: expires
}, 'sercet');*/

// console.log(token)

/*var decoded = jwt.decode('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMjMiLCJleHAiOiI4NjQwMDAwMCJ9.ogO0Jpz0q7lzVdl7AGfB55G67FnAJaLDPw3Kv4uDDqA', 'sercet');
    if (decoded.exp <= Date.now()) {
        console.log('授权错误');
    } else {
        console.log('success')
    }*/
 
 module.exports = new utils;